#!/usr/bin/perl -w

######################################################
# file : perl/diarytag.pl
# Author : Anil Kainikara (anilkumar911@gmail.com)
# Get all content with given tag.
#
# This code is licenced under the GPL.
#
######################################################

use Time::Local 'timelocal_nocheck';
#use Getopt::Std;
use Getopt::Long;
use DBI;
use strict;

my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
my @searchwords;
my @excludewords;

my @patterns;
my @tags;
my @legacytags;
my @avoidtags;
my $showfullsearch=0;

my $db = DBI->connect("dbi:SQLite:/home/anil/.local/share/diary.db", "", "",
{RaiseError => 1, AutoCommit => 1});

sub main
{
  parsesearchargs();
  search();
}
   

sub usage()
{
   print "todo : @_"
}

sub parsesearchargs
{
  Getopt::Long::Configure ('bundling');
  GetOptions('t=s'=> \@tags, 'l=s'=> \@legacytags, 'a=s'=> \@avoidtags);
}

sub search()
{
  my $sth = $db->prepare("SELECT day, month, year, content  FROM  entries order by year, month, day");

  my $rv = $sth->execute();
  my $day=0;
  my $month=0;
  my $year=0;
  my $content="";

  my $contentfound=0;
  my $searchpattern="";
  my $pattern="";
  my $legacysearchpattern="";
  my $avoidtagpattern="";
  my $line;

  my $tag;
  for $tag (@tags){
    if($searchpattern ne ""){
      $searchpattern = $searchpattern . "|" . "\<$tag *:{0,1} *[a-z 0-9]*\>.*\</$tag\>";
    } else {
      $searchpattern = "\<$tag *:{0,1} *[a-z 0-9]*\>.*\</$tag\>";
      #$searchpattern = $tag;
    }
  }

  $tag="";
  for $tag (@legacytags){
    if($legacysearchpattern ne ""){
      $legacysearchpattern = $legacysearchpattern . "|" . "\\[$tag\\]";
    } else {
      $legacysearchpattern = "\\[$tag\\]";
      #$searchpattern = $tag;
    }
  }

  $tag="";
  for $tag (@avoidtags){
    if($avoidtagpattern ne ""){
      $avoidtagpattern = $avoidtagpattern . "|" . "\\[$tag\\]";
    } else {
      $avoidtagpattern = "\\[$tag\\]";
      #$searchpattern = $tag;
    }
  }

  if ( $avoidtagpattern eq "" ){
    $avoidtagpattern = "unlikely pattern___a_ll123_mkn_098_phc";
  }

  #print "search pattern is $searchpattern \n";
  #print "legacy search pattern is $legacysearchpattern \n";
  #print "avoid tag pattern is $avoidtagpattern \n";
  while ( ($day,$month,$year,$content) =  $sth->fetchrow_array() )
  {
    my $day_of_week = get_day($month."-".$day."-".$year);
    #print $content;

    my $dateprinted=0;
    my $block="";
    if ( $searchpattern ne "" ){
      while ( $content =~ m/($searchpattern)/sig) {
         $block=$1;
         if( $dateprinted != 1 ){
           print "\n#########################################################\n";
           print "$day/$month/$year($day_of_week)\n";
           print "#########################################################\n";
           $dateprinted = 1;
         }
         print "$block \n";
       }
     }
     if ( $legacysearchpattern ne "" ){
       while ( $content =~ m/(.*)/g ){
         $line = $1;
         if ( $line =~ m/$legacysearchpattern/i ){
           if ( $line !~ m/$avoidtagpattern/i){
             #print "$legacysearchpattern found\n";
             if( $dateprinted != 1 ){
               print "\n#########################################################\n";
               print "$day/$month/$year($day_of_week)\n";
               print "#########################################################\n";
               $dateprinted = 1;
             }
             print "$line \n";
           }
         }
       }
     }
   }
}


sub get_day {
my $date = shift || return(0);
my ($mon,$mday,$year) = $date =~ /(\d+)-(\d+)-(\d+)/;
my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year);
my $day = (localtime($epochtime))[6];
return $weekday[$day];
}

main(@ARGV);
